package admin;

import java.sql.*;
import java.util.HashMap;

public class Connectmysql {
    private String url;
    private String username;
    private String password;
    private String driver;
    private Connection connection;
    private Statement statement;
    private HashMap<String, PreparedStatement> preparedStatementList = new HashMap<>();

    public Connectmysql(String url, String username, String password, String driver) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.driver = driver;
    }
    public boolean open() throws ClassNotFoundException {
        return initConnection();
    }
    private boolean initConnection() throws ClassNotFoundException {
        Class.forName(driver);
        try {
            connection = DriverManager.getConnection(this.url, this.username, this.password);
            statement = connection.createStatement();
            return true;
        } catch (SQLException e) {
            System.err.println("数据库连接期间出现异常");
            e.printStackTrace();
            return false;
        }



    }
    public ResultSet executeSql(String sql, Object[] values) {
        ResultSet resultSet = null;
        try {
            PreparedStatement statement = null;
            if (preparedStatementList.containsKey(sql)) {
                statement = preparedStatementList.get(sql);
            } else {
                statement = connection.prepareStatement(sql);
                preparedStatementList.put(sql, statement);
            }
            //System.out.println("1");
            statement.clearParameters();
            int paramIndex = 1;
            for (Object val : values) {
                statement.setObject(paramIndex, val);
                paramIndex++;
            }
            //System.out.println("2");
            resultSet = statement.executeQuery();
            //System.out.println("3");
        } catch (SQLException e) {
            System.err.println("执行查询期间出现异常:" + sql + "@" + values);
            e.printStackTrace();
        }
        return resultSet;
    }

    public ResultSet executeSqlSearch(String sql, Object[] values) {
        ResultSet resultSet = null;
        try {
            PreparedStatement statement = null;
            if (preparedStatementList.containsKey(sql)) {
                statement = preparedStatementList.get(sql);
            } else {
                statement = connection.prepareStatement(sql);
                preparedStatementList.put(sql, statement);
            }
            //System.out.println("1");
            statement.clearParameters();
            int paramIndex = 1;
            for (Object val : values) {
                statement.setObject(paramIndex, "%"+val+"%");
                paramIndex++;
            }
            //System.out.println("2");
            resultSet = statement.executeQuery();
            //System.out.println("3");
        } catch (SQLException e) {
            System.err.println("执行查询期间出现异常:" + sql + "@" + values);
            e.printStackTrace();
        }

        return resultSet;
    }



    public int executeSqlUpdate(String sql, Object[] values) {
        int resultSet=0;
        try {
            PreparedStatement statement = null;
            if (preparedStatementList.containsKey(sql)) {
                statement = preparedStatementList.get(sql);
            } else {
                statement = connection.prepareStatement(sql);
                preparedStatementList.put(sql, statement);
            }
            //System.out.println("1");
            statement.clearParameters();
            int paramIndex = 1;
            for (Object val : values) {
                statement.setObject(paramIndex, val);
                paramIndex++;
            }
            //System.out.println("2");
            resultSet = statement.executeUpdate();
            //System.out.println("3");
        } catch (SQLException e) {
            System.err.println("执行查询期间出现异常:" + sql + "@" + values);
            e.printStackTrace();
        }
        return resultSet;
    }






    public boolean close() {
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
            return true;
        } catch (SQLException e) {
            System.err.println("数据库关闭期间出现异常");
            e.printStackTrace();
            return false;
        }
    }



}
